package test;

import org.junit.jupiter.api.Test;

import java.sql.*;

class jdbc {
   @Test
   private void get2()throws ClassNotFoundException, SQLException
   {
      String sql = "SELECT * FROM t_student where name = ?";
      System.out.println("sql:" + sql);
      String URL = "jdbc:mysql://192.168.80.64:3306/soft_test?characterEncoding=utf-8";
      String USER = "root";
      String PASSWORD = "123456";
      Class.forName("com.mysql.jdbc.Driver");
      try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
         PreparedStatement preparedStatement = conn.prepareStatement(sql);
         preparedStatement.setString(1, queryName);
         try (ResultSet resultSet = preparedStatement.executeQuery()) {
            // id 2 , name 张三2, idno 350211111111111111, age 11
            while (resultSet.next()) {
               int id = resultSet.getInt("id");
               String name = resultSet.getString("name");
               String idno = resultSet.getString("idno");
               int age = resultSet.getInt("age");
               System.out.printf("id=%d,name=%s,idno=%s,age=%d\n", id, name, idno, age);
            }
         }
      }
   }

@Test
 private void get(String name)throws ClassNotFoundException, SQLException
{
   String sql="select * from t_student where name= '"+name+"'";
   System.out.println("sql:"+sql);
   String URL = "jdbc:mysql://192.168.80.64:3306/soft_test?characterEncoding=utf-8";
   String USER = "root";
   String PASSWORD = "123456";
   Class.forName("com.mysql.jdbc.Driver");
   try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)){
      conn.createStatement().execute(sql);
   }
}

    @Test
    void test() throws ClassNotFoundException, SQLException {
       String URL = "jdbc:mysql://192.168.80.64:3306/soft_test?characterEncoding=utf-8";
       String USER = "root";
       String PASSWORD = "123456";
       Class.forName("com.mysql.jdbc.Driver");
       try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
          System.out.println(conn.isClosed());
          String sql = "INSERT INTO t_student (name, idno, age) VALUES ('姚灵敏','35221111111111111',11)";
          try (Statement statement = conn.createStatement()) {
             boolean execute = statement.execute(sql);
             System.out.println(execute);
             statement.execute("UPDATE t_student SET name='updated' WHERE id = 2");
             try (ResultSet resultSet = statement.executeQuery("SELECT * FROM t_student WHERE 1=1")) {
                while (resultSet.next()) {
                   int id = resultSet.getInt("id");
                   String name = resultSet.getString("name");
                   String idno = resultSet.getString("idno");
                   int age = resultSet.getInt("age");
                   System.out.printf("id=%d,name=%s,idno=%s,age=%d\n", id, name, idno, age);
                }
             }
          }
       }
      }
 }
